# coding=utf-8
import json

from django.http import HttpResponse

from DataFactory.common.mysql_conn import SQLFunc

# Create your views here.


def select_ship_id_info_with_customer(request):
    """
    通过客户ID搜索可用的ShipID
    :param request:
    :return:
    """
    if request.method == 'POST':  # 当提交表单时
        # 判断是否传参
        if request.POST:
            env = request.POST.get('env', 0)
            user_id = request.POST.get('user_id', 0)

            conn = SQLFunc.get_cur(environment=env)["conn"]
            cur = SQLFunc.get_cur(environment=env)["cur"]

            sql_str = '''select userId,shipId,districtId,country,state,city,receiveName,contact,receiveCellphone,isDefault '''
            sql_str += '''from accord.receive_address '''
            sql_str += '''where status = 1 and (userId = '''
            sql_str += user_id
            sql_str += ' or shipId in (SELECT shipId FROM accord.erp_ship_id_related_rel where status = 1 and userId = '
            sql_str += user_id
            sql_str += '))'

            ship_id_info_list = SQLFunc.execute_sql(connection=conn, cursor=cur, sql_str=sql_str)

            return HttpResponse(json.dumps(ship_id_info_list, ensure_ascii=False), content_type="application/json,charset=utf-8")
        else:
            return HttpResponse('输入为空')

    else:
        return HttpResponse('方法错误')


def select_ship_id_info_with_ship_id(request):
    """
    通过ShipID查询ShipID信息
    :param request:
    :return:
    """
    if request.method == 'POST':  # 当提交表单时
        # 判断是否传参
        if request.POST:
            ship_id_info_list = []
            return HttpResponse(json.dumps(ship_id_info_list, ensure_ascii=False), content_type="application/json,charset=utf-8")
        else:
            return HttpResponse('输入为空')

    else:
        return HttpResponse('方法错误')


def select_latest_ten_used_upcountry_ship_id_info(request):
    """
    搜索最近使用的10个省外的ShipID
    :param request:
    """
    pass


def select_latest_ten_used_unupcountry_ship_id_info(request):
    """
    搜索最近使用的10个省内的ShipID
    :param request:
    """
    pass
